from rest_framework import serializers
from orders.models import OrderInfo, OrderGoods
from goods.models import SKU


class SKUSerialzier(serializers.ModelSerializer):
    class Meta:
        # 指定生成序列化字段的模型类
        model = SKU
        # 指定字段
        fields = ('name', 'default_image')


class OrderGoodsSerialzier(serializers.ModelSerializer):
    """
           订单商品表序列化器
    """
    # 子表嵌套父表返回
    sku = SKUSerialzier(read_only=True)

    class Meta:
        # 指定生成序列化字段的模型类
        model = OrderGoods
        # 指定字段
        fields = ('count', 'price','sku')


class OrderInfoSerialzier(serializers.ModelSerializer):
    """
        订单信息表序列化器
    """
    # 子表嵌套父表返回
    user = serializers.StringRelatedField(read_only=True)
    # 父表嵌套子表返回 订单信息表是父表 订单商品表是子表
    skus = OrderGoodsSerialzier(read_only=True,many=True)

    class Meta:
        # 指定生成序列化字段的模型类
        model = OrderInfo
        # 指定字段
        fields = "__all__"
